package com.appclub.kitsilano.settings;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;
import com.appclub.kitsilano.R;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TimePicker;

public class Settings_Mute extends SherlockActivity {

	private Spinner spinner;
	private ArrayAdapter<String> adapter;
	private Editor editor;
	private SharedPreferences settings;

	@SuppressLint("CommitPrefEdits")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting_mute);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);

		final String PREFS_NAME = "Kitsilano";
		settings = getSharedPreferences(PREFS_NAME, 0);
		editor = settings.edit();

		final int DAY1FROMHOUR = settings.getInt("DAY1FROMHOUR", 8);
		final int DAY1FROMMIN = settings.getInt("DAY1FROMMIN", 54);
		final int DAY1TOHOUR = settings.getInt("DAY1TOHOUR", 15);
		final int DAY1TOMIN = settings.getInt("DAY1TOMIN", 8);

		final int DAY2FROMHOUR = settings.getInt("DAY2FROMHOUR", 8);
		final int DAY2FROMMIN = settings.getInt("DAY2FROMMIN", 54);
		final int DAY2TOHOUR = settings.getInt("DAY2TOHOUR", 15);
		final int DAY2TOMIN = settings.getInt("DAY2TOMIN", 8);

		final int DAY1AFROMHOUR = settings.getInt("DAY1AFROMHOUR", 8);
		final int DAY1AFROMMIN = settings.getInt("DAY1AFROMMIN", 54);
		final int DAY1ATOHOUR = settings.getInt("DAY1ATOHOUR", 15);
		final int DAY1ATOMIN = settings.getInt("DAY1ATOMIN", 8);

		final int DAY2AFROMHOUR = settings.getInt("DAY2AFROMHOUR", 8);
		final int DAY2AFROMMIN = settings.getInt("DAY2AFROMMIN", 54);
		final int DAY2ATOHOUR = settings.getInt("DAY2ATOHOUR", 15);
		final int DAY2ATOMIN = settings.getInt("DAY2ATOMIN", 8);

		final int IPPDAYFROMHOUR = settings.getInt("IPPDAYFROMHOUR", 9);
		final int IPPDAYFROMMIN = settings.getInt("IPPDAYFROMMIN", 34);
		final int IPPDAYTOHOUR = settings.getInt("IPPDAYTOHOUR", 14);
		final int IPPDAYTOMIN = settings.getInt("IPPDAYTOMIN", 28);

		final TimePicker fromday1 = (TimePicker) findViewById(R.id.timePickerday11);
		final TimePicker today1 = (TimePicker) findViewById(R.id.timePickerday12);
		final Button defaultbtday1 = (Button) findViewById(R.id.defaultbtday1);
		final TimePicker fromday2 = (TimePicker) findViewById(R.id.timePickerday21);
		final TimePicker today2 = (TimePicker) findViewById(R.id.timePickerday22);
		final Button defaultbtday2 = (Button) findViewById(R.id.defaultbtday2);
		final TimePicker fromday1a = (TimePicker) findViewById(R.id.timePickerday1a1);
		final TimePicker today1a = (TimePicker) findViewById(R.id.timePickerday1a2);
		final Button defaultbtday1a = (Button) findViewById(R.id.defaultbtday1a);
		final TimePicker fromday2a = (TimePicker) findViewById(R.id.timePickerday2a1);
		final TimePicker today2a = (TimePicker) findViewById(R.id.timePickerday2a2);
		final Button defaultbtday2a = (Button) findViewById(R.id.defaultbtday2a);
		final TimePicker fromipp = (TimePicker) findViewById(R.id.timePickeripp1);
		final TimePicker toipp = (TimePicker) findViewById(R.id.timePickeripp2);
		final Button defaultbtipp = (Button) findViewById(R.id.defaultbtipp);
		final LinearLayout day1layout = (LinearLayout) findViewById(R.id.day1layout);
		final LinearLayout day2layout = (LinearLayout) findViewById(R.id.day2layout);
		final LinearLayout day1alayout = (LinearLayout) findViewById(R.id.day1alayout);
		final LinearLayout day2alayout = (LinearLayout) findViewById(R.id.day2alayout);
		final LinearLayout ipplayout = (LinearLayout) findViewById(R.id.ipplayout);
		final ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView1);

		Resources res = getResources();
		final String[] days = res.getStringArray(R.array.days);
		spinner = (Spinner) findViewById(R.id.spinner1);
		adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, days);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
		spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> arg0, View arg11,
					int arg22, long arg23) {

				if (arg22 == 0) {
					day1layout.setVisibility(View.VISIBLE);
					day2layout.setVisibility(View.GONE);
					day1alayout.setVisibility(View.GONE);
					day2alayout.setVisibility(View.GONE);
					ipplayout.setVisibility(View.GONE);
					scrollView.scrollTo(0, 0);
					fromday1.setCurrentHour(DAY1FROMHOUR);
					fromday1.setCurrentMinute(DAY1FROMMIN);
					today1.setCurrentHour(DAY1TOHOUR);
					today1.setCurrentMinute(DAY1TOMIN);
					fromday1.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
						public void onTimeChanged(TimePicker arg1, int H_D0,
								int minute0) {
							editor.putInt("DAY1FROMHOUR", H_D0);
							editor.putInt("DAY1FROMMIN", minute0);
						}
					});
					today1.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
						public void onTimeChanged(TimePicker arg1, int H_D00,
								int minute00) {
							editor.putInt("DAY1TOHOUR", H_D00);
							editor.putInt("DAY1TOMIN", minute00);
						}
					});
					arg0.setVisibility(View.VISIBLE);

					defaultbtday1
							.setOnClickListener(new View.OnClickListener() {
								public void onClick(View v) {
									fromday1.setCurrentHour(8);
									fromday1.setCurrentMinute(54);
									today1.setCurrentHour(15);
									today1.setCurrentMinute(8);
								}
							});
				}
				if (arg22 == 1) {
					day2layout.setVisibility(View.VISIBLE);
					day1layout.setVisibility(View.GONE);
					day1alayout.setVisibility(View.GONE);
					day2alayout.setVisibility(View.GONE);
					ipplayout.setVisibility(View.GONE);
					scrollView.scrollTo(0, 0);
					fromday2.setCurrentHour(DAY2FROMHOUR);
					fromday2.setCurrentMinute(DAY2FROMMIN);
					today2.setCurrentHour(DAY2TOHOUR);
					today2.setCurrentMinute(DAY2TOMIN);
					fromday2.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
						public void onTimeChanged(TimePicker arg2, int H_D1,
								int minute1) {
							editor.putInt("DAY2FROMHOUR", H_D1);
							editor.putInt("DAY2FROMMIN", minute1);
						}
					});
					today2.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
						public void onTimeChanged(TimePicker arg2, int H_D11,
								int minute11) {
							editor.putInt("DAY2TOHOUR", H_D11);
							editor.putInt("DAY2TOMIN", minute11);
						}
					});
					arg0.setVisibility(View.VISIBLE);

					defaultbtday2
							.setOnClickListener(new View.OnClickListener() {
								public void onClick(View v) {
									fromday2.setCurrentHour(8);
									fromday2.setCurrentMinute(54);
									today2.setCurrentHour(15);
									today2.setCurrentMinute(8);
								}
							});
				}
				if (arg22 == 2) {
					day1alayout.setVisibility(View.VISIBLE);
					day1layout.setVisibility(View.GONE);
					day2layout.setVisibility(View.GONE);
					day2alayout.setVisibility(View.GONE);
					ipplayout.setVisibility(View.GONE);
					scrollView.scrollTo(0, 0);
					fromday1a.setCurrentHour(DAY1AFROMHOUR);
					fromday1a.setCurrentMinute(DAY1AFROMMIN);
					today1a.setCurrentHour(DAY1ATOHOUR);
					today1a.setCurrentMinute(DAY1ATOMIN);
					fromday1a
							.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
								public void onTimeChanged(TimePicker arg3,
										int H_D2, int minute2) {
									editor.putInt("DAY1AFROMHOUR", H_D2);
									editor.putInt("DAY1AFROMMIN", minute2);
								}
							});
					today1a.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
						public void onTimeChanged(TimePicker arg3, int H_D22,
								int minute22) {
							editor.putInt("DAY1ATOHOUR", H_D22);
							editor.putInt("DAY1ATOMIN", minute22);
						}
					});
					arg0.setVisibility(View.VISIBLE);

					defaultbtday1a
							.setOnClickListener(new View.OnClickListener() {
								public void onClick(View v) {
									fromday1a.setCurrentHour(8);
									fromday1a.setCurrentMinute(54);
									today1a.setCurrentHour(15);
									today1a.setCurrentMinute(8);
								}
							});
				}
				if (arg22 == 3) {
					day2alayout.setVisibility(View.VISIBLE);
					day1layout.setVisibility(View.GONE);
					day1alayout.setVisibility(View.GONE);
					day2layout.setVisibility(View.GONE);
					ipplayout.setVisibility(View.GONE);
					scrollView.scrollTo(0, 0);
					fromday2a.setCurrentHour(DAY2AFROMHOUR);
					fromday2a.setCurrentMinute(DAY2AFROMMIN);
					today2a.setCurrentHour(DAY2ATOHOUR);
					today2a.setCurrentMinute(DAY2ATOMIN);
					fromday2a
							.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
								public void onTimeChanged(TimePicker arg4,
										int H_D3, int minute3) {
									editor.putInt("DAY2AFROMHOUR", H_D3);
									editor.putInt("DAY2AFROMMIN", minute3);
								}
							});
					today2a.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
						public void onTimeChanged(TimePicker arg4, int H_D33,
								int minute33) {
							editor.putInt("DAY2ATOHOUR", H_D33);
							editor.putInt("DAY2ATOMIN", minute33);
						}
					});
					arg0.setVisibility(View.VISIBLE);

					defaultbtday2a
							.setOnClickListener(new View.OnClickListener() {
								public void onClick(View v) {
									fromday2a.setCurrentHour(8);
									fromday2a.setCurrentMinute(54);
									today2a.setCurrentHour(15);
									today2a.setCurrentMinute(8);
								}
							});
				}
				if (arg22 == 4) {
					ipplayout.setVisibility(View.VISIBLE);
					day1layout.setVisibility(View.GONE);
					day1alayout.setVisibility(View.GONE);
					day2alayout.setVisibility(View.GONE);
					day2layout.setVisibility(View.GONE);
					scrollView.scrollTo(0, 0);
					fromipp.setCurrentHour(IPPDAYFROMHOUR);
					fromipp.setCurrentMinute(IPPDAYFROMMIN);
					toipp.setCurrentHour(IPPDAYTOHOUR);
					toipp.setCurrentMinute(IPPDAYTOMIN);
					fromipp.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
						public void onTimeChanged(TimePicker arg5, int H_D4,
								int minute4) {
							editor.putInt("IPPDAYFROMHOUR", H_D4);
							editor.putInt("IPPDAYFROMMIN", minute4);
						}
					});
					toipp.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
						public void onTimeChanged(TimePicker arg5, int H_D44,
								int minute44) {
							editor.putInt("IPPDAYTOHOUR", H_D44);
							editor.putInt("IPPDAYTOMIN", minute44);
						}

					});
					arg0.setVisibility(View.VISIBLE);

					defaultbtipp.setOnClickListener(new View.OnClickListener() {
						public void onClick(View v) {
							fromipp.setCurrentHour(9);
							fromipp.setCurrentMinute(34);
							toipp.setCurrentHour(14);
							toipp.setCurrentMinute(28);
						}
					});
				}
			}

			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});
		Button cancel = (Button) findViewById(R.id.cancel);
		cancel.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(Settings_Mute.this, Settings.class);
				startActivity(intent);
				overridePendingTransition(R.anim.bottom_in, R.anim.bottom_out);
			}
		});
		Button save = (Button) findViewById(R.id.save);
		save.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				editor.commit();
				Intent intent = new Intent(Settings_Mute.this, Settings.class);
				startActivity(intent);
				overridePendingTransition(R.anim.bottom_in, R.anim.bottom_out);
			}
		});
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			Intent intent = new Intent(this, Settings.class);
			startActivity(intent);
			overridePendingTransition(R.anim.right_in, R.anim.right_out);
			this.finish();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			Intent intent = new Intent(this, Settings.class);
			startActivity(intent);
			overridePendingTransition(R.anim.right_in, R.anim.right_out);
			this.finish();
		}
		return true;
	}
}